package com.lineage.server.clientpackets;

import com.lineage.echo.ClientExecutor;
import com.lineage.server.datatables.lock.CastleReading;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Clan;
import com.lineage.server.templates.L1Castle;
import com.lineage.server.world.WorldClan;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_Deposit extends ClientBasePacket {
  private static final Log _log = LogFactory.getLog(C_Deposit.class);
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      read(decrypt);
      int objid = readD();
      long count = readD();
      if (count > 2147483647L)
        count = 2147483647L; 
      count = Math.max(0L, count);
      L1PcInstance player = client.getActiveChar();
      if (objid == player.getId()) {
        L1Clan clan = WorldClan.get().getClan(player.getClanname());
        if (clan != null) {
          int castle_id = clan.getCastleId();
          if (castle_id != 0) {
            L1Castle l1castle = CastleReading.get().getCastleTable(castle_id);
            synchronized (l1castle) {
              long money = l1castle.getPublicMoney();
              if (player.getInventory().consumeItem(40308, count)) {
                money += count;
                l1castle.setPublicMoney(money);
                CastleReading.get().updateCastle(l1castle);
              } 
            } 
          } 
        } 
      } 
    } catch (Exception exception) {
    
    } finally {
      over();
    } 
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
